package com.kx.talk.service.impl;

import com.kx.talk.mapper.FriendRelationMapper;
import com.kx.talk.mapper.MessageDao;
import com.kx.talk.mapper.MessageMapper;
import com.kx.talk.pojo.FriendRelation;
import com.kx.talk.pojo.FriendRelationExample;
import com.kx.talk.pojo.Message;
import com.kx.talk.service.TalkService;
import com.kx.talk.vo.UnReadCountVO;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author Sun
 * @Date 2021/8/2 21:49
 */

public class TalkServiceImpl implements TalkService {
    public static final int TABLE_COUNT=2;
    @Resource
    private FriendRelationMapper friendRelationMapper;

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private MessageDao messageDao;

    @Override
    public int sendMessage(Long sendUserId, Long receiveUserId, String message, Long messageTypeId) {
        //判断sendUserId的时候，receiveUserId是否把sendUserId移除了。
        FriendRelationExample example=new FriendRelationExample();
        FriendRelationExample.Criteria criteria = example.createCriteria();
        //sendUserId代表张三
        //receiveUserId代表李四
        //张三给李四发消息，应该看看李四是否删除了张三。
        criteria.andUserIdEqualTo(receiveUserId);
        criteria.andFriendIdEqualTo(sendUserId);

        List<FriendRelation> friendRelations = friendRelationMapper.selectByExample(example);
        if(friendRelations==null||friendRelations.size()==0){
            //对方和您不是朋友关系。
            return -1;
        }
        //把消息放入数据库中
        Message msg=new Message();
        msg.setMessage(message);
        msg.setMessageTypeId(messageTypeId);
        msg.setSendTime(new Date(System.currentTimeMillis()));
        msg.setSendUserId(sendUserId);
        msg.setReceiveUserId(receiveUserId);
        msg.setReadState(0);
        msg.setIsDelete(0);
        return messageMapper.insertSelective(msg);
    }

    @Override
    public Integer modifyMessageReadState(Long sendUserId, Long receiveUserId, Long messageId, Integer readState) {
        return null;
    }

    @Override
    public List<Message> selectPageChartRecord(Long selfUserId, Long otherUserId, Long selfSendLastItemId, Long otherSendLastItemId, Integer pageSize) {
        return null;
    }

    @Override
    public long selectUnreadCountByUserId(Long userId) {
        return 0;
    }

    @Override
    public List<UnReadCountVO> selectUnReadCountGroupBy(Long userId) {
        return null;
    }
}
